package cn.new7as.cas5s.ui.login;


import android.annotation.SuppressLint;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;

import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.BuildConfig;
import cn.new7as.cas5s.R;
import cn.new7as.cas5s.tool.util.SharePreferenceUtils;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.databinding.ActivityLogininBinding;
import cn.new7as.cas5s.ui.easy.netapi.SendApi;
import cn.new7as.cas5s.ui.repay.PayForthActivity;


public class LogininActivity extends BaseActivity {

    private boolean ispwdlogin;
    private boolean CheckImg = true;
    private ActivityLogininBinding binding;
    long end;
    public int duration;
    public int count;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new SharePreferenceUtils(this).clearlogin();
        binding = ActivityLogininBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.forgetpwd.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        binding.loginPwd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (ispwdlogin) {
                    binding.loginPwd.setText(R.string.login_with_password);
                    binding.verifyme.setText("Verify Me");
                    binding.forgetpwd.setVisibility(View.GONE);
                } else {
                    binding.loginPwd.setText(R.string.verification_code_login);
                    binding.forgetpwd.setVisibility(View.VISIBLE);
                    binding.verifyme.setText("Enter");
                }
                ispwdlogin = !ispwdlogin;
            }
        });
        binding.forgetpwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.go(LogininActivity.this, ForgetPwdActivity.class);

            }
        });

        binding.clearText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.phonenum.setText("");
            }
        });
        binding.phonenum.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    count++;
                    start = System.currentTimeMillis();
                } else {
                    duration += (int) ((System.currentTimeMillis() - start) / 1000);
                }
            }
        });



        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ph = binding.phonenum.getText().toString();
                if (!CheckImg) {
                    toa("Please read the agreement and check the box");
                } else {

                    if (ph.length() == 10) {
                        if (ispwdlogin) {
                            Utils.goLogin(LogininActivity.this, PwdLoginActivity.class, ph,count,duration,System.currentTimeMillis()-start);
                        } else
                            send(binding.phonenum.getText().toString());
                    } else
                        toa(getString(R.string.phone_wrong));
                }
            }
        });
        setclickableText(binding.embark);

        binding.agreeview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CheckImg) {
                    binding.agreeview.setBackgroundResource(R.mipmap.nochoosetext);
                } else {
                    binding.agreeview.setBackgroundResource(R.mipmap.choosetext);
                }
                CheckImg = !CheckImg;
            }
        });
    }

    void send(String pho) {
        show();
        EasyHttp.post(this)
                .api(new SendApi(pho, "1")).request(new OnHttpListener<Object>() {
                    @Override
                    public void onHttpSuccess(@NonNull Object result) {
                        hide();
                        Utils.goextra(LogininActivity.this, VeriedActivity.class, binding.phonenum.getText().toString());
                        finish();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);

                    }
                });

        JsonMap jsonMap=new JsonMap();
        jsonMap.set("phone", pho);
        jsonMap.set("bizCode", "1");
        HttpRequest.build(this, "http://16.163.9.142/cbf-app/message/smsSend")

                .setJsonParameter(jsonMap)
                .setResponseListener(new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        Log.e("aaaa", response.toString());
                    }
                })
                .doPost();
    }

    private void setclickableText(TextView textView) {
        String fullText = "Before embarking on your financial journey with ZenFundBox, please take a moment to carefully review our Terms of Service and Privacy Policy. ";
        SpannableString spannableString = new SpannableString(fullText);

        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Utils.goextra(LogininActivity.this, PayForthActivity.class, BuildConfig.TERM);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.GREEN);

            }
        };
        ClickableSpan clickableSpanoth = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Utils.goextra(LogininActivity.this, PayForthActivity.class, BuildConfig.PRI);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.GREEN);

            }
        };
        spannableString.setSpan(clickableSpan, 105, 121, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(clickableSpanoth, 126, fullText.length() - 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }

}